package eteam.aps.controller;

import eteam.aps.common.R;
import eteam.aps.common.exceptions.BusinessException;
import eteam.aps.common.helper.Helper;
import eteam.aps.model.dto.PackagingWarehouseDto;
import eteam.aps.model.entity.MocoRders;
import eteam.aps.model.vo.moco_rders.MocoRdersDisplayVo;
import eteam.aps.service.PackagingWarehousingService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @author LiuLei
 */
@Api(tags = "包装入库")
@RestController
@RequestMapping("/pwc")
public class PackagingWarehousingController {

    @Autowired
    private PackagingWarehousingService packagingWarehousingService;

    @PostMapping("/gmr")
    @ApiOperation("扫描包装产品信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "scan", value = "产品编号"),
            @ApiImplicitParam(name = "quteType", value = "包装类型【0 内包装，1 外包装】")
    })
    public R<MocoRdersDisplayVo> getMocoRdersPackage(String scan, Integer quteType) throws Exception {
        if (Helper.isEmpty(scan))
            throw new BusinessException(HttpStatus.BAD_REQUEST, "产品编号不能为空！");
        if (quteType == null) quteType = 1;
        MocoRdersDisplayVo moc = packagingWarehousingService.getMocoRdersPackage(scan, quteType);
        if (moc == null) throw new BusinessException(HttpStatus.BAD_REQUEST, "暂无数据！");
        return R.success(moc);
    }

    @PostMapping("/gmpst")
    @ApiOperation("根据条码号查询该任务令对应的数据统计")
    @ApiImplicitParam(name = "scan", value = "产品编号")
    public R<Map<String, Integer>> getMocoRdersPackageScanSummary(String scan) throws Exception {
        if (Helper.isEmpty(scan))
            throw new BusinessException(HttpStatus.BAD_REQUEST, "产品编号不能为空！");
        return R.success(packagingWarehousingService.getMocoRdersPackageScanSummary(scan));
    }

    @PostMapping("/upt")
    @ApiOperation("修改包装扫描状态")
    @ApiImplicitParam(name = "scan", value = "产品编号")
    public R<Integer> updatePackagePackType(String scan) throws Exception {
        if (Helper.isEmpty(scan))
            throw new BusinessException(HttpStatus.BAD_REQUEST, "产品编号不能为空！");
        return R.success(packagingWarehousingService.updatePackagePackType(scan));
    }

    @PostMapping("/mrlbq")
    @ApiOperation("查询（包装/入库）产品信息")
    @ApiImplicitParam(name = "scan", value = "产品编号")
    public R<List<MocoRdersDisplayVo>> getMocoRdersLisByQuery(@RequestBody PackagingWarehouseDto packagingWarehouseDto) throws Exception {
        if (Helper.isEmpty(packagingWarehouseDto.getStartTime()))
            throw new BusinessException(HttpStatus.BAD_REQUEST, "请输入开始时间！");
        if (Helper.isEmpty(packagingWarehouseDto.getEndTime()))
            throw new BusinessException(HttpStatus.BAD_REQUEST, "请输入结束时间！");
        List<MocoRdersDisplayVo> list = packagingWarehousingService.getMocoRdersLisByQuery(packagingWarehouseDto);
        if (list.isEmpty()) throw new BusinessException(HttpStatus.BAD_REQUEST, "暂无数据！");
        return R.success(list);
    }

    @PostMapping("/gmrw")
    @ApiOperation("扫描入库产品信息")
    @ApiImplicitParam(name = "scan", value = "产品编号")
    public R<MocoRders> getMocoRdersWarehousing(String scan) {
        if (Helper.isEmpty(scan))
            throw new BusinessException(HttpStatus.BAD_REQUEST, "产品编号不能为空！");
        MocoRders moc = packagingWarehousingService.getMocoRdersWarehousing(scan);
        if (moc == null) throw new BusinessException(HttpStatus.BAD_REQUEST, "暂无数据！");
        return R.success(moc);
    }

    @PostMapping("/spw")
    @ApiOperation("包装入库")
    @ApiImplicitParam(name = "map", value = "生产入库")
    public R<String> savePackagingWarehousing(@RequestBody Map<String, Object> map) throws Exception {
        if (map.isEmpty()) throw new BusinessException(HttpStatus.BAD_REQUEST, "参数非法异常！");
        return R.success(packagingWarehousingService.savePackagingWarehousing(map));
    }

}
